﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Moonwish.Silverlight.OsAvatar.Programs;
using Moonwish.Silverlight.OsAvatar.Utility;

namespace Moonwish.Silverlight.OsAvatar
{
    /// <summary>
    /// 系统桌面，桌面程序、开始菜单、任务栏等的实现。
    /// write by zhlin 2011-11-20
    /// </summary>
    public partial class Desktop : UserControl
    {
        private readonly ProgramCollection _programs = new ProgramCollection(null);
        private DispatcherTimer _dispatcherTimer = new DispatcherTimer();

        public Desktop()
        {
            InitializeComponent();

            ProgramWrapper.SetEnvironment(CnvDesktop, StkPanelTask); ;

            _dispatcherTimer.Tick += DispatcherTimerTick;
            _dispatcherTimer.Interval = TimeSpan.FromSeconds(1.0);
            _dispatcherTimer.Start();
            Loaded += Desktop_Loaded;
        }

        void Desktop_Loaded(object sender, RoutedEventArgs e)
        {
            ListboxDesktop.ItemsSource = ProgramWrapper.DesktopPrograms;

            tvStartMenu.ItemsSource = Programs;
        }

        public ProgramCollection Programs
        {
            get { return _programs; }
        }

        /// <summary>
        /// 头像图片
        /// </summary>
        public ImageSource AvatarImage
        {
            get { return (ImageSource)GetValue(AvatarImageProperty); }
            set { SetValue(AvatarImageProperty, value); }
        }

        /// <summary>
        /// 头像图片依赖属性
        /// </summary>
        public static readonly DependencyProperty AvatarImageProperty = DependencyProperty.Register("AvatarImage",
                                                                                                    typeof(ImageSource),
                                                                                                    typeof(Desktop),
                                                                                                    new PropertyMetadata
                                                                                                        (new BitmapImage
                                                                                                             (new Uri(
                                                                                                                  "/Moonwish.Silverlight.OsAvatar;component/UserTitle/10.png",
                                                                                                                  UriKind.Relative))));
        /// <summary>
        /// 桌面程序图标单击事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MouseClickManagerClick(object sender, MouseButtonEventArgs e)
        {
        }

        /// <summary>
        /// 桌面程序图标双击事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MouseClickManagerDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var p = (ProgramWrapper)((Button)sender).DataContext;
            p.Show();
        }

        /// <summary>
        /// 开始菜单外边点击控制。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BrdStartMenuContainerMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            brdStartMenuContainer.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// 禁止开始菜单向上冒泡事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BrdStartMenuMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }
        /// <summary>
        /// 时间与网络连接检测。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DispatcherTimerTick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbClock.Text))
            {
                ToolTipService.SetToolTip(tbClock, DateTime.Now.ToLongDateString());
            }
            tbClock.Text = DateTime.Now.ToShortTimeString();
            CheckNetwork();
        }

        /// <summary>
        /// 网格检测。
        /// </summary>
        private void CheckNetwork()
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                ToolTipService.SetToolTip(imgNetwork, "Connected");
                imgNetwork.Source = new BitmapImage(new Uri("/Moonwish.Silverlight.OsAvatar;component/Icons/network.png", UriKind.RelativeOrAbsolute));
            }
            else
            {
                ToolTipService.SetToolTip(imgNetwork, "Disconnected");
                imgNetwork.Source = new BitmapImage(new Uri("/Moonwish.Silverlight.OsAvatar;component/Icons/nonetwork.png", UriKind.RelativeOrAbsolute));
            }
        }

        /// <summary>
        /// 显示桌面。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnShowDesktopClick(object sender, RoutedEventArgs e)
        {
            
        }

        /// <summary>
        /// 注销按钮事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnlockClick(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 退出系统按钮事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnShutdownClick(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 右程序菜单事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void RightStartMenuSelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        /// <summary>
        /// 程序搜索框按钮事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void TxtSearchTextChanged(object sender, TextChangedEventArgs e)
        {

        }

        /// <summary>
        /// 开始程序菜单项选择事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void TvStartMenuSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var tv = (TreeView) sender;
            var p = (ProgramWrapper)e.NewValue;
            if (p.Children.Count > 0)
            {
                var node = tv.GetSelectedContainer();
                node.IsExpanded = !node.IsExpanded;
            }
            else
            {
                p.Show();
                brdStartMenuContainer.Visibility = Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 开始按钮点击事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BtnStartMenuClick(object sender, RoutedEventArgs e)
        {
            if (brdStartMenuContainer.Visibility == Visibility.Collapsed)
            {
                brdStartMenu.Height = brdStartMenu.MaxHeight;
                if (brdStartMenu.Height > ((ActualHeight - rectTaskBar.ActualHeight) - 10.0))
                {
                    brdStartMenu.Height = (ActualHeight - rectTaskBar.ActualHeight) - 10.0;
                }
                txtSearch.Text = string.Empty;
                //lbRightStartMenu.ItemsSource = ProgramManager.GetRightStartMenuPrograms;
                brdStartMenuContainer.Visibility = Visibility.Visible;
                //WindowsManager.ArrangeActiveWindow(false);
            }
            else
            {
                brdStartMenuContainer.Visibility = Visibility.Collapsed;
                //WindowsManager.ArrangeActiveWindow(true);
            }
        }

        /// <summary>
        /// 桌面图标按钮事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void BtnDesktopDoubleClick(object sender, EventArgs e)
        {
            var p = (ProgramWrapper)((Button)sender).DataContext;
            p.Show();
        }

        /// <summary>
        /// 桌面按钮容器获得焦点事件。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DesktopContainerGotFocus(object sender, RoutedEventArgs e)
        {

        }
    }
}
